{include file="common/header"/}
<div class="lemo-container">
    <div class="lemo-main">
        <div class="admin-main layui-anim layui-anim-upbit">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-normal" id="importExcel">导入Excel</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" id="delAll">批量删除</button>
            </div>
            <table id="customer" lay-filter="customer"></table>
        </div>
    </div>
</div>
{include file="common/footer"/}

<script type="text/html" id="toolbar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
</script>

<script>
    layui.use(['table', 'form', 'layer', 'upload'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var upload = layui.upload;
        var $ = layui.jquery;

        // 初始化表格
        var tableIn = table.render({
            elem: '#customer',
            url: 'customer',
            page: true,
            cols: [[
                {type: 'checkbox'},
                {field: 'id', title: 'ID', width: 80, sort: true},
                {field: 'name', title: '客户名称'},
                {field: 'phone', title: '联系电话'},
                {field: 'address', title: '地址'},
                {field: 'create_time', title: '创建时间', sort: true},
                {title: '操作', toolbar: '#toolbar', width: 150}
            ]]
        });

        // 导入Excel按钮点击事件
        $('#importExcel').on('click', function(){
            layer.open({
                type: 1,
                title: '导入Excel',
                area: ['400px', '200px'],
                content: `
                    <div class="layui-form" style="padding: 20px;">
                        <div class="layui-form-item">
                            <div class="layui-upload">
                                <button type="button" class="layui-btn" id="uploadExcel">选择文件</button>
                                <div class="layui-upload-list">
                                    <p id="uploadFileName"></p>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button type="button" class="layui-btn layui-btn-normal" id="submitImport">开始导入</button>
                        </div>
                    </div>
                `,
                success: function(layero, index){
                    var uploadInst = upload.render({
                        elem: '#uploadExcel',
                        url: 'importExcel',
                        accept: 'file',
                        exts: 'xlsx|xls',
                        before: function(obj){
                            layer.load();
                        },
                        done: function(res){
                            layer.closeAll('loading');
                            if(res.code === 0){
                                layer.msg('上传成功', {icon: 1});
                                $('#uploadFileName').text(res.data.filename);
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        },
                        error: function(){
                            layer.closeAll('loading');
                            layer.msg('上传失败', {icon: 2});
                        }
                    });

                    // 提交导入
                    $('#submitImport').on('click', function(){
                        var filename = $('#uploadFileName').text();
                        if(!filename){
                            layer.msg('请先选择文件', {icon: 2});
                            return;
                        }
                        layer.confirm('确定要导入数据吗？', function(index){
                            layer.close(index);
                            layer.load();
                            $.post('importExcel', {filename: filename}, function(res){
                                layer.closeAll('loading');
                                if(res.code === 0){
                                    layer.msg('导入成功', {icon: 1});
                                    layer.closeAll();
                                    tableIn.reload();
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            });
                        });
                    });
                }
            });
        });

        // 其他现有代码...
    });
</script> 